﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.IO;
using UICommon.CommonEnum;

namespace UICommon.ExtendHtmlHelper
{
    public static class ExtendBaseFile
    {
        public static bool HasFile(this HttpPostedFileBase file)
        {
            return (file != null && file.ContentLength > 0) ? true : false;
        }

        public static bool CheckImage(this HttpPostedFileBase file)
        {
            if (!file.HasFile())
            {
                return false;
            }

            String fileTypes = "gif,jpg,bmp,jpeg,png,tiff,tif";

            String fileExt = Path.GetExtension(file.FileName).ToLower();

            if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                return false;
            }

            return true;
        }

        public static UICommon.CommonEnum.CoursewareType GetFileTypeByExtension(string extension)
        {
            string pictures = "gif,jpg,bmp,jpeg,png,tiff,tif";
            extension = extension.Substring(1).ToLower();

            if (!(Array.IndexOf(pictures.Split(','), extension) == -1))
            {
                return CommonEnum.CoursewareType.Picture;
            }

            string mp3 = "wav,mp3,wma";
            if (!(Array.IndexOf(mp3.Split(','), extension) == -1))
            {
                return CommonEnum.CoursewareType.MP3;
            }

            string video = "mp4,avi,mov,asf,wmv,rm,rmvb,mkv,flv,WebM,mpg,m1v,movie,mp2,mpa,mpe,mpeg,qt,dat";
            if (!(Array.IndexOf(video.Split(','), extension) == -1))
            {
                return CommonEnum.CoursewareType.Video;
            }

            string flash = "swf";
            if (!(Array.IndexOf(flash.Split(','), extension) == -1))
            {
                return CommonEnum.CoursewareType.Flash;
            }

            string ppt = "ppt,key,pptx";
            if (!(Array.IndexOf(ppt.Split(','), extension) == -1))
            {
                return CommonEnum.CoursewareType.PPT;
            }

            string other = "xls,xlsx,numbers,doc,docx,txt,pages,zip,rar";
            if (!(Array.IndexOf(other.Split(','), extension.Substring(1).ToLower()) == -1))
            {
                return CommonEnum.CoursewareType.Other;
            }

            return CommonEnum.CoursewareType.Other;
        }

        public static string GetExtensionByCoursewareType(UICommon.CommonEnum.CoursewareType type)
        {
            string extension = "";

            switch (type)
            {
                case CommonEnum.CoursewareType.Flash:
                    extension = "swf";
                    break;

                case CommonEnum.CoursewareType.MP3:
                    extension = "wav,mp3,wma";
                    break;

                case CommonEnum.CoursewareType.Other:
                    extension = "xls,xlsx,numbers,doc,docx,txt,pages,zip,rar";
                    break;

                case CommonEnum.CoursewareType.Picture:
                    extension = "gif,jpg,bmp,jpeg,png,tiff,tif";
                    break;

                case CommonEnum.CoursewareType.PPT:
                    extension = "ppt,key,pptx";
                    break;

                case CommonEnum.CoursewareType.Video:
                    extension = "mp4,avi,mov,asf,wmv,rm,rmvb,mkv,flv,WebM,mpg,m1v,movie,mp2,mpa,mpe,mpeg,qt,dat";
                    break;
                    
            }
            return extension;
        }

        public static string GetExtensionByCoursewareTypeForSql(CoursewareType coursewareType)
        {
            string extension = UICommon.ExtendHtmlHelper.ExtendBaseFile.GetExtensionByCoursewareType(coursewareType);

            if (string.IsNullOrEmpty(extension))
            {
                return "";
            }

            StringBuilder returnStr = new StringBuilder(50);

            for (int i = 0; i < extension.Split(',').Length; i++)
            {
                returnStr.AppendFormat("'.{0}',", extension.Split(',')[i]);
            }

            return returnStr.ToString().TrimEnd(',');
        }

        public static bool CheckVideo(string extension)
        {
            string video = ".mp4,.avi,.mov,.asf,.wmv,.rm,.rmvb,.mkv,.flv,.webm,.mpg,.m1v,.movie,.mp2,.mpa,.mpe,.mpeg,.qt,.dat";

            if (!(Array.IndexOf(video.Split(','), extension.ToLower()) == -1))
            {
                return true;
            }

            return false;
        }

        public static bool CheckMP3(string extension)
        {
            string video = ".mp3";

            if (!(Array.IndexOf(video.Split(','), extension.ToLower()) == -1))
            {
                return true;
            }

            return false;
        }

        public static bool CheckFlash(string extension)
        {
            string video = ".swf";

            if (!(Array.IndexOf(video.Split(','), extension.ToLower()) == -1))
            {
                return true;
            }

            return false;
        }

        public static bool CheckExcept(string extension)
        {
            string video = ".exe,.bat";

            if (!(Array.IndexOf(video.Split(','), extension) == -1))
            {
                return true;
            }

            return false;
        }

        public static string CheckVideoType(string extension)
        {
            if (CheckVideo(extension.ToLower()))
            {
                return "video";
            }
            if (CheckMP3(extension.ToLower()))
            {
                return "mp3";
            }
            if (CheckFlash(extension.ToLower()))
            {
                return "flash";
            }

            return "";
        }

        public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
        {
            var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                         select new { ID = e, Name = e.ToString() };
            return new SelectList(values, "Id", "Name", enumObj);
        }

        #region 章节ui层递归

        public static Action<T> Fix<T>(Func<Action<T>, Action<T>> f)
        {
            return x => f(Fix(f))(x);
        }

        public static void Render<T>(this HtmlHelper helper, T model, Func<Action<T>, Action<T>> f)
        {
            Fix(f)(model);
        }

        public static void Render<T>(this HtmlHelper helper, T model, SelfApplicable<T> f)
        {
            f(f, model);
        }

        #endregion
    }

    public delegate void SelfApplicable<T>(SelfApplicable<T> self, T arg);
}
